// Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 

package jagex.client;

import jagex.DataOperations;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.io.IOException;

public class GameWindowMiddleMan extends GameWindow {

	public void login(String username, String password) {
		try {
			user = username;
			username = DataOperations.addCharacter(username, 20);
			pass = password;
			password = DataOperations.addCharacter(password, 20);
			if (username.trim().length() == 0 || password.trim().length() == 0) {
				drawText(loginResponses[0], loginResponses[1]);
				return;
			}
			drawText(loginResponses[6], loginResponses[7]);

			streamClass = new StreamClass(makeSocket(mainServer, 43594), this);

			streamClass.createPacket(0);
			streamClass.addTwo4ByteInts(DataOperations
					.stringLength12ToLong(username));
			streamClass.addString(password);
			streamClass.add2ByteInt(xc);
			streamClass.finalisePacket();
			int loginResponse = streamClass.readInputStream();
			System.out.println("Login response: " + loginResponse);
			if (loginResponse == 0) {
				reset();
				resetVars();
				return;
			}
			if (loginResponse == 2) {
				drawText(loginResponses[8], loginResponses[9]);
				return;
			}
			if (loginResponse == 3) {
				drawText(loginResponses[10], loginResponses[11]);
				return;
			}
			if (loginResponse == 4) {
				drawText(loginResponses[4], loginResponses[5]);
				return;
			}
			if (loginResponse == 5) {
				drawText(loginResponses[16], loginResponses[17]);
				return;
			} else {
				drawText(loginResponses[12], loginResponses[13]);
				return;
			}
		} catch (Exception exception) {
			mainServer = backupServer;
			exception.printStackTrace();
			drawText(loginResponses[12], loginResponses[13]);
			return;
		}
	}

	public void newPlayer(String arg0, String arg1, String arg2, int arg3,
			int arg4, int arg5) {
		try {
			streamClass = new StreamClass(makeSocket(mainServer, 43594), this);
			streamClass.createPacket(2);
			arg0 = DataOperations.addCharacter(arg0, 20);
			arg1 = DataOperations.addCharacter(arg1, 20);
			streamClass.addTwo4ByteInts(DataOperations
					.stringLength12ToLong(arg0));
			streamClass.addString(arg1);
			for (; arg2.length() < 40; arg2 = arg2 + " ") {
				;
			}
			streamClass.addString(arg2);
			streamClass.add4ByteInt(arg3);
			streamClass.add4ByteInt(arg4);
			streamClass.add4ByteInt(arg5);
			streamClass.formatPacket();
			streamClass.read2ByteInt();
			int i = streamClass.readInputStream();
			streamClass.closeStream();
			System.out.println("Newplayer response: " + i);
			if (i == 0) {
				hb();
				return;
			}
			if (i == 2) {
				drawText(loginResponses[8], loginResponses[9]);
				return;
			}
			if (i == 3) {
				drawText(loginResponses[14], loginResponses[15]);
				return;
			}
			if (i == 4) {
				drawText(loginResponses[4], loginResponses[5]);
				return;
			}
			if (i == 5) {
				drawText(loginResponses[16], loginResponses[17]);
				return;
			} else {
				drawText(loginResponses[12], loginResponses[13]);
				return;
			}
		} catch (Exception exception) {
			mainServer = backupServer;
			System.out.println(String.valueOf(exception));
			drawText(loginResponses[12], loginResponses[13]);
			return;
		}
	}

	public void sendLogoutPacket() {
		try {
			if (streamClass != null) {
				streamClass.createPacket(1);
				streamClass.finalisePacket();
				user = "";
				pass = "";
				resetIntVars();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void reconnect(String arg0, String arg1) {
		user = arg0;
		arg0 = DataOperations.addCharacter(arg0, 20);
		pass = arg1;
		arg1 = DataOperations.addCharacter(arg1, 20);
		if (arg0.length() == 0 || arg1.length() == 0) {
			resetIntVars();
			return;
		}
		for (long l = System.currentTimeMillis(); System.currentTimeMillis()
				- l < 60000L;) {
			s(loginResponses[2], loginResponses[3]);
			try {
				streamClass = new StreamClass(makeSocket(mainServer, 43594),
						this);
				streamClass.createPacket(19);
				streamClass.addTwo4ByteInts(DataOperations
						.stringLength12ToLong(arg0));
				streamClass.addString(arg1);
				streamClass.add2ByteInt(xc);
				streamClass.finalisePacket();
				int i = streamClass.readInputStream();
				if (i == 0) {
					reset();
					q();
					return;
				}
				if (i >= 1 && i <= 4) {
					arg0 = "";
					arg1 = "";
					resetIntVars();
					return;
				}
			} catch (Exception _ex) {
				s(loginResponses[2], loginResponses[3]);
			}
			s(loginResponses[2], loginResponses[3]);
			try {
				Thread.sleep(5000L);
			} catch (Exception _ex) {
			}
		}

		arg0 = "";
		arg1 = "";
		resetIntVars();
	}

	public void reconnect() {
		System.out.println("Lost connection");
		reconnect(user, pass);
	}

	public void s(String arg0, String arg1) {
		Graphics g = getGraphics();
		Font font = new Font("Helvetica", 1, 15);
		int i = ej();
		int j = oj();
		g.setColor(Color.black);
		g.fillRect(i / 2 - 140, j / 2 - 25, 280, 50);
		g.setColor(Color.white);
		g.drawRect(i / 2 - 140, j / 2 - 25, 280, 50);
		zi(g, arg0, font, i / 2, j / 2 - 10);
		zi(g, arg1, font, i / 2, j / 2 + 10);
	}

	public void reset() {
		length = 0;
		packetID = 0;
		wc = -500;
		friendsCount = 0;
	}

	public void resetLastPing() {
		long now = System.currentTimeMillis();
		lastPing = now;
	}

	protected final void sendPingPacketReadPacketData() {
		long l = System.currentTimeMillis();
		if (streamClass.containsData()) {
			lastPing = l;
		}
		if (l - lastPing > 5000L) {
			lastPing = l;
			streamClass.createPacket(5);
			streamClass.formatPacket();
		}
		try {
			streamClass.writePacket(20);
		} catch (IOException _ex) {
			reconnect();
			return;
		}
		int packetLength = streamClass.readPacket(packetData);
		if (packetLength > 0) {
			checkIncomingPacket(packetData[0] & 0xff, packetLength);
		}
	}

	protected final void checkIncomingPacket(int command, int pLength) {
		if (command == 8) {
			String s = new String(packetData, 1, pLength - 1);
			handleServerMessage(DataOperations.censor(s, true));
		} else if (command == 23) {
			friendsCount = DataOperations.getUnsignedByte(packetData[1]);
			for (int i = 0; i < friendsCount; i++) {
				friendListLongs[i] = DataOperations.getUnsigned8Bytes(
						packetData, 2 + i * 9);
				friendListOnlineStatus[i] = DataOperations
						.getUnsignedByte(packetData[10 + i * 9]);
			}
		} else if (command == 24) {
			long l1 = DataOperations.getUnsigned8Bytes(packetData, 1);
			int i1 = packetData[9] & 0xff;
			for (int j1 = 0; j1 < friendsCount; j1++) {
				if (friendListLongs[j1] == l1) {
					if (friendListOnlineStatus[j1] == 0 && i1 != 0) {
						handleServerMessage("@pri@"
								+ DataOperations.longToString(l1)
								+ " has logged in");
					}
					if (friendListOnlineStatus[j1] != 0 && i1 == 0) {
						handleServerMessage("@pri@"
								+ DataOperations.longToString(l1)
								+ " has logged out");
					}
					friendListOnlineStatus[j1] = i1;
					pLength = 0;
					return;
				}
			}
			friendListLongs[friendsCount] = l1;
			friendListOnlineStatus[friendsCount] = i1;
			friendsCount++;
			handleServerMessage("@pri@" + DataOperations.longToString(l1)
					+ " has been added to your friends list");
		} else if (command == 26) {
			ignoreListCount = DataOperations.getUnsignedByte(packetData[1]);
			for (int j = 0; j < ignoreListCount; j++) {
				ignoreListLongs[j] = DataOperations.getUnsigned8Bytes(
						packetData, 2 + j * 8);
			}

		} else if (command == 27) {
			hideOnlineStatus = packetData[1];
			blockChatMessages = packetData[2];
			blockPrivateMessages = packetData[3];
			blockTradeRequests = packetData[4];
			sd = packetData[5];
		} else if (command == 28) {
			long l2 = DataOperations.getUnsigned8Bytes(packetData, 1);
			String s2 = new String(packetData, 9, pLength - 9);
			if (l2 != DataOperations.stringLength12ToLong(user)) {
				s2 = DataOperations.censor(s2, true);
			}
			handleServerMessage("@pri@" + DataOperations.longToString(l2)
					+ ": tells you " + s2);
		} else {
			handleIncomingPacket(command, pLength, packetData);
		}
		pLength = 0;
		return;
	}

	public void ab(String arg0) {
		arg0 = DataOperations.addCharacter(arg0, 20);
		streamClass.createPacket(25);
		streamClass.addString(arg0);
		streamClass.formatPacket();
	}

	public void nb(int arg0, int arg1, int arg2, int arg3, int arg4) {
		streamClass.createPacket(31);
		streamClass.addByte(arg0);
		streamClass.addByte(arg1);
		streamClass.addByte(arg2);
		streamClass.addByte(arg3);
		streamClass.addByte(arg4);
		streamClass.formatPacket();
	}

	public void lb(String arg0) {
		long l = DataOperations.stringLength12ToLong(arg0);
		streamClass.createPacket(29);
		streamClass.addTwo4ByteInts(l);
		streamClass.formatPacket();
		for (int i = 0; i < ignoreListCount; i++) {
			if (ignoreListLongs[i] == l) {
				return;
			}
		}

		if (ignoreListCount >= 50) {
			return;
		} else {
			ignoreListLongs[ignoreListCount++] = l;
			return;
		}
	}

	public void cb(long arg0) {
		streamClass.createPacket(30);
		streamClass.addTwo4ByteInts(arg0);
		streamClass.formatPacket();
		for (int i = 0; i < ignoreListCount; i++) {
			if (ignoreListLongs[i] == arg0) {
				ignoreListCount--;
				for (int j = i; j < ignoreListCount; j++) {
					ignoreListLongs[j] = ignoreListLongs[j + 1];
				}

				return;
			}
		}

	}

	public void gb(String arg0) {
		streamClass.createPacket(26);
		streamClass.addTwo4ByteInts(DataOperations.stringLength12ToLong(arg0));
		streamClass.formatPacket();
	}

	public void removeFriend(long arg0) {
		streamClass.createPacket(27);
		streamClass.addTwo4ByteInts(arg0);
		streamClass.formatPacket();
		for (int i = 0; i < friendsCount; i++) {
			if (friendListLongs[i] != arg0) {
				continue;
			}
			friendsCount--;
			for (int j = i; j < friendsCount; j++) {
				friendListLongs[j] = friendListLongs[j + 1];
				friendListOnlineStatus[j] = friendListOnlineStatus[j + 1];
			}

			break;
		}

		handleServerMessage("@pri@" + DataOperations.longToString(arg0)
				+ " has been removed from your friends list");
	}

	public void privateMessage(long arg0, String arg1) {
		if (arg1.length() > 80) {
			arg1 = arg1.substring(0, 80);
		}
		streamClass.createPacket(28);
		streamClass.addTwo4ByteInts(arg0);
		streamClass.addByte(arg1.length());
		streamClass.addString(arg1);
		streamClass.formatPacket();
		handleServerMessage("@pri@You tell "
				+ DataOperations.longToString(arg0) + ": " + arg1);
	}

	public boolean isTellCommand(String arg0) {
		if (arg0.toLowerCase().startsWith("tell ")) {
			arg0 = arg0.substring(5);
			int i = arg0.indexOf(' ');
			if (i == -1 || i >= arg0.length() - 1) {
				handleServerMessage("You must type a message too!");
				return true;
			} else {
				String s1 = arg0.substring(0, i);
				arg0 = arg0.substring(i + 1);
				privateMessage(DataOperations.stringLength12ToLong(s1), arg0);
				return true;
			}
		}
		streamClass.createPacket(3);
		streamClass.addString(arg0);
		streamClass.formatPacket();
		lastPing = hd = System.currentTimeMillis();
		if (uc) {
			handleServerMessage("@yel@" + user.trim() + ": @whi@" + arg0);
		}
		return false;
	}

	public void drawText(String s1, String s2) {
	}

	public void q() {
	}

	protected void resetVars() {
	}

	public void resetIntVars() {
	}

	public void hb() {
	}

	protected void handleIncomingPacket(int i, int j, byte abyte0[]) {
	}

	public void handleServerMessage(String s1) {
	}

	public boolean jb() {
		return true;
	}

	public GameWindowMiddleMan() {
		mainServer = "127.0.0.1";
		backupServer = "127.0.0.1";
		port = 43594;
		user = "";
		pass = "";
		packetData = new byte[5000];
		friendListLongs = new long[50];
		friendListOnlineStatus = new int[50];
		ignoreListLongs = new long[50];
	}

	public static String loginResponses[];
	public static boolean uc = true;
	public static int vc = 0x5f5e0ff;
	public static int wc;
	public static int xc = 1;
	public String mainServer;
	public String backupServer;
	public int port;
	String user;
	String pass;
	public StreamClass streamClass;
	int length;
	int packetID;
	byte packetData[];
	long hd;
	long lastPing;
	public int friendsCount;
	public long friendListLongs[];
	public int friendListOnlineStatus[];
	public int ignoreListCount;
	public long ignoreListLongs[];
	public int hideOnlineStatus;
	public int blockChatMessages;
	public int blockPrivateMessages;
	public int blockTradeRequests;
	public int sd;

	static {
		loginResponses = new String[50];
		loginResponses[0] = "You must enter both a username";
		loginResponses[1] = "and a password - Please try again";
		loginResponses[2] = "Connection lost! Please wait...";
		loginResponses[3] = "Attempting to re-establish";
		loginResponses[4] = "That username is already in use";
		loginResponses[5] = "Wait 60 seconds then retry";
		loginResponses[6] = "Please wait...";
		loginResponses[7] = "Connecting to server";
		loginResponses[8] = "Sorry! The server is currently full";
		loginResponses[9] = "Please try again later";
		loginResponses[10] = "Invalid username or password";
		loginResponses[11] = "Try again, or create a new account";
		loginResponses[12] = "Sorry! Unable to connect to server";
		loginResponses[13] = "Please check your internet settings";
		loginResponses[14] = "Username already taken";
		loginResponses[15] = "Please choose another username";
		loginResponses[16] = "The client has been updated";
		loginResponses[17] = "Please clear your cache and reload";
	}
}
